package cn.dujc.superkid.activities;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseListActivity;
import cn.dujc.superkid.adapter.ChannelAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.ChannelEntity;
import cn.dujc.superkid.utils.Preferences;

/**
 * @author du
 * date 2018/8/1 下午11:29
 */
public class ChannelActivity extends BaseListActivity {
    private static final String TAG = "ChannelActivity";
    private final List<ChannelEntity> mList = new ArrayList<>();

    @Nullable
    @Override
    protected BaseQuickAdapter initAdapter() {
        return new ChannelAdapter(mList);
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        super.initBasic(savedInstanceState);
        setTitle("选择频道");
    }

    @Override
    protected void onItemClick(int position) {
        final ChannelEntity channelEntity = mList.get(position);
        Preferences.getInstance().saveChannel(mActivity, channelEntity);
        setResult(RESULT_OK);
        finish();
    }

    @Override
    protected void loadMore() {
        refreshDone();
        loadDone(true, true);
    }

    @Override
    protected void reload() {
        loadData();
    }

    private void loadData() {
        HttpHelper.getHome().channel(QMap.build().create())
                .enqueue(new ResultCallBack<Result<List<ChannelEntity>>>(mActivity) {
                    @Override
                    public void onSuccessCallback(int code, Result<List<ChannelEntity>> body) {
                        if (body != null) {
                            final List<ChannelEntity> data = body.getData();
                            if (data != null) {
                                Log.d(TAG, "onSuccessCallback: data="+data.toString());
                                mList.addAll(data);
                            }
                        }
                        notifyDataSetChanged(true);
                    }

                    @Override
                    public void onFailureCallback(int code, String msg) {
                        super.onFailureCallback(code, msg);
                        loadFailure();
                    }
                });
    }
}
